package com.seal.commons.core.bo;


import com.seal.commons.copiers.Copiers;
import com.seal.commons.copiers.base.Copier;
import com.seal.commons.core.entity.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

/**
 * 功能描述: <br/>
 *
 * @author huangyong
 * @date: 2020-06-01 21:21<br/>
 * @since JDK 1.8
 */

public class BOSupport<ID extends Serializable> implements Serializable {

    private static final long serialVersionUID = -3195033174155483607L;

    private ID id;

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    /**
     * 使用默认BeanMapper拷贝
     * @param obj
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void parse(Object obj) {
        Copier copier = Copiers.createOrika(obj.getClass(), this.getClass()).register();
        copier.copy(obj, this);
    }

    public <E extends BaseEntity> E toEntity(Class<? extends BaseEntity> clazz){
        Copier copier = Copiers.createCglib(this.getClass(), clazz, (value,cls,setter) -> value);
        return (E) copier.copy(this);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

